from wxauto import *
import webot2 as webot
import time
from queue import Queue
from threading import Thread

me = 'G.L.Wang'
allowing_people = '文件传输助手' #'文件传输助手'#'Lady Gaga'

wx = WeChat()
wx.ChatWith(allowing_people)

def sends(msg):
    WxUtils.SetClipboard(msg)
    wx.SendClipboard()
    return wx.GetLastMessage[2]


try:
    webot.test =  False
    webot.start()
    msg = wx.GetLastMessage
    cmd = "注意：现在由机器人接管微信！\r\n请注意不要谈论隐私信息！\r\nAttention: WeChat is now being taken over by a robot! \r\nPlease be careful not to discuss any private information!"
    _msg_stamp = sends(cmd)
    _msg_stamp = sends('发送"#help"查看帮助信息\r\nsend "#help" to get help\r\n')
    try:
        print('starting...')
        answer = webot.make_a_question(f'你好，我是{allowing_people}。')
    except Exception as e:
        print(e)
        sends(f'  启动失败！Failed to start! {e}')
        webot.quit()
        print('exit')
        exit(-1)
    msg = f'  {answer}'
    _msg_stamp = sends(msg)
    while "i'm pretty":
        who, msg, timestamp = wx.GetLastMessage
        if timestamp == _msg_stamp:
            time.sleep(0.5)
            continue
        if who != allowing_people and who != me:
            _msg_stamp = timestamp
            continue
        cmd = msg.strip()
        if cmd == '#exit' or cmd == '#bye' or cmd == '#quit':
            webot.close()
            _msg_stamp = sends('  Bye~')
            break
        if cmd == '#reload':
            webot.reload()
            _msg_stamp = sends('  Reloaded.')
            continue
        if msg == '#help':
            msg = '#help:\t显示帮助\tshow help\r\n#exit:\t退出\t exit chat\r\n#new:\t开始新话题\tstart a new topic'
            _msg_stamp = sends(msg)
            continue
        if msg == '#new':
            webot.new_topic()
            _msg_stamp = timestamp
            continue
        try:
            msg = webot.make_a_question(msg)
            msg = f'  {msg}'
            print(msg)
        except Exception as e:
            webot.reload()
            msg = f"  抱歉，发生了一些错误。\r\nSorry, something went wrong.\r\n{e}"
        _msg_stamp = sends(msg)
        time.sleep(0.5)
except Exception as e:
    print(e)
    print('exit')
